home *** CD-ROM | disk | FTP | other *** search
/ 8bitfiles.net/archives / archives.tar / archives / compuserve-file-archive / 11 Exotic Applications / ASCII.ZIP / PET_TRUE.C next >
Encoding:
C/C++ Source or Header  |  1991-06-08  |  3.1 KB  |  101 lines

  1. /* Get PET ASCII characters from the source file and convert them 
  2.    to True ASCII, then write them to the target file.  Stop when an 
  3.    EOF (End of File) is encountered.
  4.    
  5.    Called with:
  6.    
  7. PET-TRUE  Pet_ASCII_file   True_ASCII_file
  8.  
  9.    Noel Nyman 3/90
  10.  
  11. */
  12.  
  13. #include <stdio.h>
  14. #include <ctype.h>
  15. #include <io.h>
  16.  
  17. char sourcebuf[BUFSIZ * 16];                   /* source file buffer */
  18. char targetbuf[BUFSIZ * 16];                   /* target file buffer */
  19.  
  20. main(argc, argv)
  21.         int argc;
  22.         char *argv[];
  23.  
  24.  
  25. {
  26.         int c, i=0;
  27.         FILE *source, *target;
  28.         char response='Y';
  29.         char petfile[128], truefile[128];
  30.                 
  31.  
  32.         if (argc == 2)
  33.             {    
  34.             printf("PET-TRUE: must specify both source and target file\n");
  35.             exit(1);
  36.             }
  37.         else if (argc == 1)
  38.             {
  39.             printf("File name of PET-ASCII file: ");
  40.             gets(petfile);
  41.             printf("File name of destination file: ");
  42.             gets(truefile);
  43.             }
  44.         else 
  45.             {
  46.             strcpy(petfile,argv[1]);
  47.             strcpy(truefile,argv[2]);
  48.             }
  49.                 
  50.  
  51.         if (access(truefile, 0) == 0)   /* does target exist */
  52.                 do
  53.                     {
  54.                     printf("\n\"%s\" already exists - Overwrite (Y/N)? ", truefile);
  55.                     response = getchar();
  56.                     while (getchar() != '\n')
  57.                         { ; }
  58.                     response = toupper(response);
  59.                     }         /* loop until Y or N */
  60.                 while (response != 'Y' && response != 'N');
  61.  
  62.         if (response == 'Y')    /* overwrite the file */
  63.                 {
  64.                 if ((source = fopen(petfile, "r")) == NULL)
  65.                         printf("PET-TRUE: error opening \"%s\"\n", petfile);
  66.                 
  67.                 else if ((target = fopen(truefile, "w")) == NULL)
  68.                         printf("PET-TRUE: error opening \"%s\"\n", truefile);
  69.  
  70.                 else
  71.                     { 
  72.                     setvbuf( source, sourcebuf, _IOFBF, sizeof( sourcebuf ) );
  73.                     setvbuf( target, targetbuf, _IOFBF, sizeof( targetbuf ) );
  74.  
  75.                     printf("Working .");
  76.                     while ((c = getc(source)) != EOF)
  77.                        {
  78.                          if isupper(c) 
  79.                                putc(tolower(c), target);
  80.  
  81.                          else if (c == 13)      /* handle single return */
  82.                                putc('\n', target);
  83.  
  84.                          else 
  85.                                putc(((c>=193)&(c<=218)?(c-=128):c),target);
  86.                          
  87.                          if (++i > 256)
  88.                                 {
  89.                                  i = 0;
  90.                                  printf(".");
  91.                                 }
  92.                         }
  93.                       fclose(source);
  94.                       fclose(target);
  95.                       printf("\n");
  96.                     }
  97.                 }
  98.    }
  99.  
  100.  
  101.